package com.ruoyi.business.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.School;
import com.ruoyi.business.service.ISchoolService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学校Controller
 * 
 * @author lcz
 * @date 2025-02-20
 */
@Api(tags = "学校相关接口")
@RestController
@RequestMapping("/business/school")
public class SchoolController extends BaseController
{
    @Autowired
    private ISchoolService schoolService;

    /**
     * 查询学校列表
     */
    @ApiIgnore
//    @PreAuthorize("@ss.hasPermi('business:school:list')")
    @GetMapping("/list")
    public TableDataInfo list(School school)
    {

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long userId = sysUser.getUserId();
        if (userId != 1) {
            school.setId(sysUser.getSchoolId());
        }

        startPage();
        List<School> list = schoolService.selectSchoolList(school);
        return getDataTable(list);
    }

    /**
     * 导出学校列表
     */
    @ApiIgnore
//    @PreAuthorize("@ss.hasPermi('business:school:export')")
    @Log(title = "学校", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, School school)
    {
        List<School> list = schoolService.selectSchoolList(school);
        ExcelUtil<School> util = new ExcelUtil<School>(School.class);
        util.exportExcel(response, list, "学校数据");
    }

    /**
     * 获取学校详细信息
     */
    @ApiIgnore
//    @PreAuthorize("@ss.hasPermi('business:school:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(schoolService.selectSchoolById(id));
    }

    /**
     * 新增学校
     */
    @ApiIgnore
//    @PreAuthorize("@ss.hasPermi('business:school:add')")
    @Log(title = "学校", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody School school)
    {
        return toAjax(schoolService.insertSchool(school));
    }

    /**
     * 修改学校
     */
    @ApiIgnore
//    @PreAuthorize("@ss.hasPermi('business:school:edit')")
    @Log(title = "学校", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody School school)
    {
        return toAjax(schoolService.updateSchool(school));
    }

    /**
     * 删除学校
     */
    @ApiIgnore
//    @PreAuthorize("@ss.hasPermi('business:school:remove')")
    @Log(title = "学校", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(schoolService.deleteSchoolByIds(ids));
    }

    /** 前端接口 ***************************************************************************************************/
    /** 前端接口 ***************************************************************************************************/
    /** 前端接口 ***************************************************************************************************/

    /**
     * 查询学校列表
     * 接口路径：/school/front/list
     */
    @GetMapping("/front/list")
    @ApiOperation("查询所有学校列表")
    public R<List<School>> frontList()
    {
        School school = new School();
        List<School> list = schoolService.selectSchoolList(school);
        return R.ok(list);
    }
//
//    /**
//     * 获取学校详细信息
//     * 接口路径：/school/front/info
//     */
//    @ApiOperation("前台获取学校详细信息")
//    @GetMapping(value = "/front/info")
//    public R<School> frontInfo(@RequestParam("id") Long id)
//    {
//        return R.ok(schoolService.selectSchoolById(id));
//    }
//
//    /**
//     * 新增学校
//     * 接口路径：/school/front/add
//     */
//    @ApiOperation("前台新增学校")
//    @PostMapping("/front/add")
//    public AjaxResult frontAdd(@RequestBody School school)
//    {
//        return toAjax(schoolService.insertSchool(school));
//    }
//
//    /**
//     * 修改学校
//     * 接口路径：/school/front/edit
//     */
//    @ApiOperation("前台修改学校")
//    @PostMapping("/front/edit")
//    public AjaxResult frontEdit(@RequestBody School school)
//    {
//        return toAjax(schoolService.updateSchool(school));
//    }
//
//    /**
//     * 删除学校
//     * 接口路径：/school/front/delete
//     */
//    @ApiOperation("前台删除学校")
//    @GetMapping("/front/delete")
//    public AjaxResult frontRemove(@RequestParam Long[] ids)
//    {
//        return toAjax(schoolService.deleteSchoolByIds(ids));
//    }
}
